﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Reporting.WinForms;

namespace Peak.ApplicationServices.Reports
{
  public class StoryPDFGenerator : IStoryPDFGenerator
  {
    List<MemoryStream> streams = new List<MemoryStream>();
    private int _currentPage;

    private const string PDFA4DeviceInfo = "<DeviceInfo>"
                                           + "  <OutputFormat>PDF</OutputFormat>"
                                           + "  <PageWidth>8.5in</PageWidth>"
                                           + "  <PageHeight>11in</PageHeight>"
                                           + "  <MarginTop>1cm</MarginTop>"
                                           + "  <MarginLeft>1cm</MarginLeft>"
                                           + "  <MarginRight>1cm</MarginRight>"
                                           + "  <MarginBottom>1cm</MarginBottom>"
                                           + "</DeviceInfo>";   

    public byte[] Render<T>(IEnumerable<T> data, string reportPath)
    {      
      LocalReport report = new LocalReport();
      report.ReportEmbeddedResource = reportPath;
      report.DataSources.Add(new ReportDataSource("Peak_Core_Story", data.ToArray()));
      
      string mimeType;
      string encoding;
      string fileNameExtension;
      string[] streams;
      Warning[] warnings;

      return report.Render(
          "PDF",
          PDFA4DeviceInfo,
          out mimeType,
          out encoding,
          out fileNameExtension,
          out streams,
          out warnings);      
    }

    public string ReportPath
    {
      get 
      {
        return "Peak.ApplicationServices.Reports.Story.rdlc";
      }
    }
  }
}